mainStage = JSGF.inherit(JSGF.Stage);

mainStage.prototype.onLoad = function() {
	var self = this;
	
	this.is_drawing = false;
	
	this.mapData = [
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1
	];

	this.testtext = JSGF.factory(JSGF.SpriteText, "テスト");
	this.testtext.setZIndex(10000);
	this.testtext.color = "black";
	this.testtext.height = 32;
	this.testtext.move(100, 100);
	this.appendSprite(this.testtext);
	
	this.testtext2 = JSGF.factory(JSGF.SpriteText, "2");
	this.testtext2.setZIndex(10000);
	this.testtext2.color = "black";
	this.testtext2.height = 32;
	this.testtext2.move(100, 132);
	this.appendSprite(this.testtext2);

	this.obj = new Array();
//	var stage = new Stage();
//	stage.w = this.getWidth();
//	stage.h = this.getHeight();	
//	this.obj.push(stage);

	var bar = new Bar();
	bar.x = Math.floor(this.getWidth() / 2) - bar.w/2;
	bar.y = this.getHeight()-100;
	bar.append(this);
	this.obj.push(bar);
	
	for (var i = 0; i < 1; i++) {
		var rand, rand2;
		rand = Math.floor(Math.random()*100)-50;
		var ball = new Ball();
		ball.x = Math.floor(this.getWidth() / 2)+rand;
		rand = Math.floor(Math.random()*100)-50;
		ball.y = this.getHeight()-150 + rand;
		rand = (Math.random()*2)-1;
//		rand2 = (Math.random()*2)-1;
		ball.setVec(rand, -1);
		ball.append(this);
		this.obj.push(ball);
	}
	
	for (var i = 0; i < this.mapData.length; i++) {
		switch(this.mapData[i]) {
		case 1:
			var block = new Block();
			// pos = (index ? disp) * (size + margin);
			block.x = (i%10) * (50);	block.y = Math.floor(i/10) * (25+1);
			block.w = 50;				block.h = 25;
			block.append(this);
			this.obj.push(block);
		default:
			break;
		}
	}
	

	var stage = new Block();
	stage.x = -100; stage.y = 0; stage.w =　100, stage.h = this.getHeight()+1;
	stage.str = -1; this.obj.push(stage);
	stage = new Block();
	stage.x = 0; stage.y = this.getHeight(); stage.w =　this.getWidth()+1, stage.h = 100;
	stage.str = -1; this.obj.push(stage);
	stage = new Block();
	stage.x = this.getWidth(); stage.y = 0; stage.w =　100, stage.h = this.getHeight()+1;
	stage.str = -1; this.obj.push(stage);
	stage = new Block();
	stage.x = 0; stage.y = -100; stage.w =　this.getWidth()+1, stage.h = 100;
	stage.str = -1; this.obj.push(stage);
	

	this.drawUpdate();
	this.startAnimation(16);

}

mainStage.prototype.onUnload = function() {
	alert("stage unload");
}

mainStage.prototype.onDraw = function(dt) {
	// エッジキーチェック
	edge_key = pls_key & (pls_key ^ pls_prev);
	pls_prev = pls_key
//	if (!edge_key) { return;}
	// 移動処理等
	this.action();
	// 衝突判定
	this.check_collision();
	// 描画処理等
	if (!this.is_drawing) {
		this.is_drawing = true;

		// テスト表示
		this.testtext.setText(pls_key.toString(2));
		this.testtext2.setText(edge_key.toString(2));
		
		this.drawing();
		
		this.is_drawing = false;
	}
}

mainStage.prototype.onKeyDown = function(e) {
	e.keyCode = (e.keyCode)?e.keyCode:e;
	for (var i = 0; i < key_conf.length; i++) {
		if (e.keyCode == key_conf[i]) {
			pls_key |= (1 << i);
		}
	}
}

mainStage.prototype.onKeyUp = function(e){
	e.keyCode = (e.keyCode)?e.keyCode:e;
	for (var i = 0; i < key_conf.length; i++) {
		if (e.keyCode == key_conf[i]) {
			pls_key ^= (1 << i);
		}
	}
}

mainStage.prototype.onNoCanvas = function() {
	//TODO message
}

mainStage.prototype.check_collision = function() {
	// ballとそれ以外の当たり判定チェックは分別するよ
	var ball_list = new Array();
	for (var i = 0; i < this.obj.length; i++) {
		for(var j = 0; j < this.obj.length; j++) {
			var my = this.obj[i];
			var op = this.obj[j];
			if((my.type & OBJ_TYPE_BALL) != 0) {
				ball_list.push(my);
				break;
			}
			if ( i != j ) {
				if (my.target_type & op.type) {
					// 衝突判定
					if (my.hitcheck(op)) {
						// ヒットコールバック
						my.hit_callback(op, true);
						op.hit_callback(my, false);
					}
				}
			}
		}
	}
	// Ball専用の当たり判定
	var move_continue = null;
	do{
		move_continue = new Array();
		for (var i = 0; i < ball_list.length; i++) {
			for(var j = 0; j < this.obj.length; j++) {
				var my = ball_list[i];
				var op = this.obj[j];
				// TODO 自分自身との当たり判定の無条件除外処理
				if (true) {
					if (my.target_type & op.type) {
						// 衝突判定
						if (my.hitcheck(op)) {
//							 ヒットコールバック
							if (!my.is_hit) {
								my.is_hit = true;
								move_continue.push(my);
							}
						}
					}
				}
			}
		}
		// 対象とのヒットコールバック
		for (var i = 0; i < move_continue.length; i++) {
			var my = move_continue[i];
			var op = my.hit_obj;
			my.hit_callback(op, true);
			if(op.hit_callback(my, false)) {
				for(var j = 0; j < this.obj.length; j++) {
					if (this.obj[j] == op) {
						this.obj.splice(j, 1);
						break;
					}
				}
				this.removeSprite(op.img);
			}
		}
		ball_list = move_continue;
	}while(ball_list.length > 0);
}

mainStage.prototype.action = function() {
	for (var i = 0; i < this.obj.length; i++) {
		this.obj[i].action();
	}
}

mainStage.prototype.drawing = function() {
	for (var i = 0; i < this.obj.length; i++) {
		this.obj[i].drawing();
	}
}